<#assign content>
<div class="right_col" role="main">
    <div class="">
        <div class="page-title">
            <div class="title_left">
                <h3>原料列表</h3>
            </div>
            <div class="title_right">
            </div>
        </div>
        <div class="clearfix"></div>
        <div class="row">
            <div class="col-md-12 col-sm-12 col-xs-12">
                <div class="x_panel">
                    <div class="x_title">
                        <h2>
                            <small></small>
                        </h2>
                        <ul class="nav navbar-right panel_toolbox">
                            <li><a id="materialAdd"><i class="fa fa-plus-circle"></i></a></li>
                            <li><a class="collapse-link"><i class="fa fa-chevron-up"></i></a>
                            </li>
                        </ul>
                        <div class="clearfix"></div>
                    </div>
                    <div class="x_content">
                        <p class="text-muted font-13 m-b-30">

                        </p>
                        <table id="materialtable" class="table table-striped table-bordered">
                            <thead>
                            <tr>
                                <th>原料名</th>
                                <th>计算单位</th>
                                <th>状态</th>
                                <th>操作</th>
                            </tr>
                            </thead>

                            <tfoot>
                            <tr>
                                <th>原料名</th>
                                <th>计算单位</th>
                                <th>状态</th>
                                <th>操作</th>
                            </tr>
                            </tfoot>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</#assign>
<#include "../_inc/layout.ftl" />
<#--原料新增-->
<div class="modal fade" id="materialAddModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
     aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content" style="width: 700px;">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">原料新增</h4>
            </div>
            <form id="materialAddForm" method="post" novalidate class="form-horizontal form-label-left">
                <div class="modal-body">
                    <div class="item form-group">
                        <label class="control-label col-md-3">原料名<span class="required">*</span>
                        </label>
                        <div class="col-md-5">
                            <input type="text" id="addname" name="addname" data-validate-length-range="5,12"
                                   placeholder="请输入原料名" required="required" class="form-control col-md-6">
                        </div>
                    </div>
                </div>
                <div class="modal-body">
                    <div class="item form-group">
                        <label class="control-label col-md-3">原料单位<span class="required">*</span>
                        </label>
                        <div class="col-md-5">
                            <input type="text" id="addunit" name="addunit" data-validate-length-range="5,12"
                                   placeholder="请输入原料单位" required="required" class="form-control col-md-6">
                        </div>
                    </div>
                </div>
                <div class="modal-body">
                    <div class="item form-group">
                        <label class="control-label col-md-3">所属类别<span class="required">*</span>
                        </label>
                        <div class="col-md-5">
                            <select name="addsign" id="addsign" class="form-control col-md-6" required>
                                <option value="">---请选择---</option>
                                <option value="0">基础原料</option>
                                <option value="1">基础配方</option>
                            </select>
                        </div>
                    </div>
                </div>
                <div class="ln_solid"></div>
                <div class="form-group">
                    <div class="col-md-8 col-md-offset-9">
                        <button id="saveMaterial" type="button" class="btn btn-success">保存</button>
                        <button class="btn btn-primary" type="reset">重置</button>
                    </div>
                </div>
            </form>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>
<#--原料修改-->
<div class="modal fade" id="materialEditModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
     aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content" style="width: 700px;">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">原料修改</h4>
            </div>
            <form id="materialEditForm" method="post" novalidate class="form-horizontal form-label-left">
                <input type="hidden" id="edit_materialid" name="id">
                <div class="modal-body">
                    <div class="item form-group">
                        <label class="control-label col-md-3">原料名<span class="required">*</span>
                        </label>
                        <div class="col-md-5">
                            <input type="text" id="editname" name="editname" data-validate-length-range="5,12"
                                   placeholder="请输入原料名" required="required" class="form-control col-md-6">
                        </div>
                    </div>
                </div>
                <div class="modal-body">
                    <div class="item form-group">
                        <label class="control-label col-md-3">原料型号<span class="required">*</span>
                        </label>
                        <div class="col-md-5">
                            <input type="text" id="editunit" name="editunit" data-validate-length-range="5,12"
                                   placeholder="请输入原料单位" required="required" class="form-control col-md-6">
                        </div>
                    </div>
                </div>
                <div class="ln_solid"></div>
                <div class="form-group">
                    <div class="col-md-8 col-md-offset-9">
                        <button id="saveEditMaterial" type="button" class="btn btn-success">保存</button>
                        <button class="btn btn-primary" type="reset">重置</button>
                    </div>
                </div>
            </form>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>
<#--原料删除-->
<div class="modal fade" id="materialDeleteModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
     aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content" style="width: 700px;">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">原料删除</h4>
            </div>
            <form id="materialDeleteForm" method="post" novalidate class="form-horizontal form-label-left">
                <input type="hidden" id="delete_materialid" name="id">
                <div class="modal-body">
                    <div class="item form-group">
                        <div class="col-md-10">
                            <span id="materialDeleteMessage"></span>
                        </div>
                    </div>
                </div>
                <div class="ln_solid"></div>
                <div class="form-group">
                    <div class="col-md-8 col-md-offset-9">
                        <button id="saveDeleteMaterial" type="button" class="btn btn-success">确定</button>
                        <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    </div>
                </div>
            </form>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>

<script>
    var materialtable;

    $(document).ready(function () {
        materialtable = $('#materialtable').DataTable({
            lengthChange: false,            // 是否允许用户自定义显示数量
            bPaginate: true,                // 翻页功能
            bFilter: false,                 // 列筛序功能
            ordering: false,                // 排序功能
            Info: true,                     // 页脚信息
            autoWidth: true,                // 自动宽度
            pageLength: 10,                 // 每页显示10条数据
            oLanguage: {                    // 国际语言转化
                oAria: {
                    sSortAscending: ': 以升序排列此列',
                    sSortDescending: ': 以降序排列此列'
                },
                sLengthMenu: '显示 _MENU_ 记录',
                sZeroRecords: '对不起，查询不到任何相关数据',
                sEmptyTable: '未有相关数据',
                sLoadingRecords: '正在加载数据...',
                sInfo: '当前显示 _START_ 到 _END_ 条，共 _TOTAL_ 条记录。',
                sInfoEmpty: '当前显示0到0条，共0条记录。',
                sSearch: '查询:',
                oPaginate: {
                    sFirst: '首页',
                    sPrevious: ' 上一页 ',
                    sNext: ' 下一页 ',
                    sLast: ' 尾页 '
                }
            },
            stripeClasses: ["odd", "even"],                         //为奇偶行加上样式，兼容不支持CSS伪类的场合
            paginationType: 'full_numbers',
            serverSide: true,
            ajax: function (data, callback, settings) {
                var param = {};                                     // 封装请求参数
                param.limit = data.length;                          // 页面显示记录条数，在页面显示每页显示多少项的时候
                param.start = data.start;                           // 开始的记录序号
                param.page = (data.start / data.length) + 1;        // 当前页码
                param.action = 'page';
                $.ajax({
                    type: 'POST',
                    url: '/material_list.do',
                    cache: false,                                   //禁用缓存
                    data: param,                                    //传入组装的参数
                    dataType: 'json',
                    success: function (result) {
                        if (result['success']) {
                            // 封装返回数据
                            var returnData = {};
                            returnData.recordsTotal = result['recordsTotal'];              // 返回数据全部记录
                            returnData.recordsFiltered = result['recordsFiltered'];        // 后台不实现过滤功能，每次查询均视作全部结果
                            returnData.data = result['records'];                           // 返回的数据列表
                            callback(returnData);
                        } else {
                            $.promptModal('${warning}', result['msg'], result.logout);
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        $.promptModal('${warning}', '${systemException}', false);
                    }
                });
            },
            columns: [
                {data: 'name'},
                {data: 'unit'},
                {
                    data: 'flag',
                    render: function (data, type, row, meta) {
                        if (data) {
                            return '<span style="color: #00CC33">正常</span>';
                        } else {
                            return '<span style="color: #be2626">已删除</span>';
                        }
                    }
                },
                {
                    data: 'id',
                    render: function (data, type, row, meta) {
                        var str = '';
                        if(row.flag){
                            str += '<button onclick="$.editMaterial(\'' + row.id + '\')" type="button" class="btn btn-success btn-xs"><i class="fa fa-pencil"></i>修改</button>';
                            str += '<button onclick="$.deleteMaterial(\'' + row.id + '\')" type="button" class="btn btn-danger btn-xs"><i class="fa fa-trash-o"></i>删除</button>';
                            if(row.sign == 1) {
                                str += '<button onclick="$.basicMaterial(\'' + row.id + '\')" type="button" class="btn btn-info btn-xs"><i class="fa fa-forumbee"></i>原料明细</button>';
                            }
                        }
                        return str;
                    }
                }
            ],
            columnDefs: [
                {orderable: false, targets: '_all'},
                {defaultContent: '', targets: '_all'}
            ]
        });

        //跳转到原料明细页面
        $.basicMaterial = function (id) {
            var param = {};
            param.basicid = id;
            $.submitFormPost('/basicsmate_list.do', param, 'POST');
        }

        // 用户新增
        $('#materialAdd').on('click', function () {
            $('#materialAddModal').modal({
                keyboard: true
            });
        });

        // 窗体关闭清空页面
        $('#materialAddModal').on('hide.bs.modal', function () {
            $('#materialAddForm')[0].reset();
        });

        // 窗体关闭清空页面
        $('#materialEditModal').on('hide.bs.modal', function () {
            $('#materialEditForm')[0].reset();
        });

        // 提交保存
        $('#saveMaterial').on('click', function () {
            if (validator.checkAll($('#materialAddForm'))) {
                var param = {};
                param.action = 'do';
                param.name = $('#addname').val();
                param.unit = $('#addunit').val();
                param.sign = $('#addsign').val();
                $('#materialAddForm')[0].reset();
                $('#materialAddModal').modal('hide');
                $.ajax({
                    type: 'POST',
                    url: '/material_add.do',
                    cache: false,
                    dataType: 'json',
                    data: param,
                    success: function (result) {
                        if (result.success) {
                            materialtable.draw(false);
                        }
                        else {
                            $.promptModal('${warning}', result.msg, result.logout);
                            setTimeout(function () {
                                location.reload();
                            },1500);
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        $.promptModal('${warning}', '${systemException}', false);
                    }
                });
            }
        });

        // 修改
        $.editMaterial = function (id) {
            var param = {};
            param.action = 'edit';
            param.id = id;
            $.ajax({
                type: 'POST',
                url: '/material_edit.do',
                cache: false,
                dataType: 'json',
                data: param,
                success: function (result) {
                    if (result.success) {
                        $('#edit_materialid').val(id);
                        $('#editname').val(result.body.sMaterial.name);
                        $('#editunit').val(result.body.sMaterial.unit);
                        $('#materialEditModal').modal({
                            keyboard: true
                        });
                    }
                    else {
                        $.promptModal('${warning}', result.msg, result.logout);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    $.promptModal('${warning}', '${systemException}', false);
                }
            });
        };

        // 保存修改
        $('#saveEditMaterial').on('click', function () {
            if (validator.checkAll($('#materialEditForm'))) {
                var param = {};
                param.action = 'doEdit';
                param.id = $('#edit_materialid').val();
                param.name = $('#editname').val();
                param.unit = $('#editunit').val();
                $('#materialEditForm')[0].reset();
                $('#materialEditModal').modal('hide');
                $.ajax({
                    type: 'POST',
                    url: '/material_edit.do',
                    cache: false,
                    dataType: 'json',
                    data: param,
                    success: function (result) {
                        if (result.success) {
                            materialtable.draw(false);
                        }
                        else {
                            $.promptModal('${warning}', result.msg, result.logout);
                            setTimeout(function () {
                                location.reload();
                            },1500);
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        $.promptModal('${warning}', '${systemException}', false);
                    }
                });
            }
        });

        // 删除
        $.deleteMaterial = function (id) {
            $('#delete_materialid').val(id);
            $('#materialDeleteMessage').html('确定删除原料信息？');
            $('#materialDeleteModal').modal({
                keyboard: true
            });
        };

        // 保存删除
        $('#saveDeleteMaterial').on('click', function () {
            var param = {};
            param.action = 'doDelete';
            param.id = $('#delete_materialid').val();
            $('#materialDeleteModal').modal('hide');
            $.ajax({
                type: 'POST',
                url: '/material_edit.do',
                cache: false,
                dataType: 'json',
                data: param,
                success: function (result) {
                    if (result.success) {
                        materialtable.draw(false);
                    }
                    else {
                        $.promptModal('${warning}', result.msg, result.logout);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    $.promptModal('${warning}', '${systemException}', false);
                }
            });
        });
    });
</script>

